﻿//-----------------------------------------------------------------------
// <copyright file="TestConfigReader.cs" company="None">
//     Copyright None. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace ODataRegression.Framework.Core.Shared
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using ODataRegression.Framework.Core.Shared;
    using ODataTestFrameworkCore.shared;

    public static class TestConfigReader
    {
        ////Path List
        private static string xpathTestTypeSettings = "//TestTypeSettings";
        private static string xpathIterationSettings = "//IterationSettings";
        private static string xpathTestEngineSettings = "//TestEngineSettings";
        private static string xpathTestResultSettings = "//TestResultSettings";
        private static string xpathServiceRoot = "//ServiceRoot";
        private static string xpathURLMapperActionList = "//URLMapperSettings/MappingActions";
        private static string xPathRepeatCount="//IterationSettings/RepeatCount";

        //// Property holders 
        private static bool isInitComplete = false;
        private static string testConfigFileName;
        private static XmlDocument configXML;
        private static XmlNodeList urlMapper;
        private static Dictionary<ODataTestNames, XmlAttributeCollection> requestedODataTest;
        private static Dictionary<TestEngineSettingsTypes, string> testEngineSettings = null;
        private static XmlNodeList servicesEndPoints = null;
        private static Dictionary<TestResultSettingsTypes, string> testResultSettings = null;
        private static List<XmlNode> iterationSettings = null;
        private static int repeatCount = -1;
        // Public Properties 
        public static XmlNodeList UrlMapperActions
        {
            get
            {
                if (urlMapper == null)
                {
                    urlMapper = configXML.DocumentElement.SelectSingleNode(xpathURLMapperActionList).ChildNodes;
                }

                return urlMapper;
            }
        }
        
        public static Dictionary<ODataTestNames, XmlAttributeCollection> RequestedODataTest
        {
            get
            {
                if (requestedODataTest == null)
                {
                    requestedODataTest = new Dictionary<ODataTestNames, XmlAttributeCollection>();
                    
                    XmlNode xmlTestTypeSettings = configXML.DocumentElement.SelectSingleNode(xpathTestTypeSettings);
                    foreach (XmlNode item in xmlTestTypeSettings.ChildNodes)
                    {
                        if (item.NodeType == XmlNodeType.Element && Convert.ToBoolean(item.InnerText))
                        {
                            RequestedODataTest.Add((ODataTestNames)Enum.Parse(typeof(ODataTestNames), item.LocalName), item.Attributes);
                        }
                    }
                }

                return requestedODataTest;
            }
        }

        public static Dictionary<TestEngineSettingsTypes, string> TestEngineSettings
        {
            get
            {
                if (testEngineSettings == null)
                {
                    testEngineSettings = new Dictionary<TestEngineSettingsTypes, string>();

                    XmlNode engineSettings = configXML.DocumentElement.SelectSingleNode(xpathTestEngineSettings);
                    foreach (XmlNode item in engineSettings.ChildNodes)
                    {
                        if (item.NodeType == XmlNodeType.Element)
                        {
                            testEngineSettings.Add((TestEngineSettingsTypes)Enum.Parse(typeof(TestEngineSettingsTypes), item.LocalName), item.InnerText);
                        }
                    }
                }

                return testEngineSettings;
            }
        }

        public static XmlNodeList ServicesEndPoints
        {
            get
            {
                if (servicesEndPoints == null)
                {
                    servicesEndPoints = configXML.DocumentElement.SelectSingleNode(xpathServiceRoot).ChildNodes;
                }

                return servicesEndPoints;
            }
        }

        public static Dictionary<TestResultSettingsTypes, string> TestResultSettings
        {
            get
            {
                if (testResultSettings == null)
                {
                    testResultSettings = new Dictionary<TestResultSettingsTypes, string>();

                    XmlNode engineSettings = configXML.DocumentElement.SelectSingleNode(xpathTestResultSettings);

                    if (engineSettings != null)
                    {
                        foreach (XmlNode item in engineSettings.ChildNodes)
                        {
                            if (item.NodeType!=XmlNodeType.Comment)
                            {
                                testResultSettings.Add((TestResultSettingsTypes)Enum.Parse(typeof(TestResultSettingsTypes), item.LocalName), item.InnerText);
                            }
                            
                        }
                    }
                }

                return testResultSettings;
            }
        }

        public static List<XmlNode> IterationSettings
        {
            get
            {
                if (iterationSettings == null)
                {
                    iterationSettings = new List<XmlNode>();
                    XmlNode xmliterationSettings = configXML.DocumentElement.SelectSingleNode(xpathIterationSettings);
                    if (iterationSettings != null)
                    {
                        foreach (XmlNode iterateOverNode in xmliterationSettings.ChildNodes)
                        {
                            if (iterateOverNode.NodeType == XmlNodeType.Element)
                            {
                                iterationSettings.Add(iterateOverNode);
                            }
                        }
                    }
                }

                return iterationSettings;
            }
        }


        public static long RepeatCount
        {
            get
            {
                if (repeatCount <0)
                {
                    repeatCount = 0;
                    XmlNode xmlRepeatCountSettings = configXML.DocumentElement.SelectSingleNode(xPathRepeatCount);
                    if (xmlRepeatCountSettings != null)
                    {
                        repeatCount=Convert.ToInt32(xmlRepeatCountSettings.InnerText);
                       
                    }
                }

                return repeatCount;
            }
        }
        // Init Function
        public static void Intit(string xmlConfigFile)
        {
            if (!isInitComplete)
            {
                testConfigFileName = xmlConfigFile;
                configXML = new XmlDocument();
                configXML.Load(testConfigFileName);
               
            }
        }
    }
}
